package com.ihd.ihardware.view.tzc.discovery.view;

import android.databinding.ObservableArrayList;
import android.os.Bundle;

import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.DiscoveryFxFragmentBinding;
import com.ihd.ihardware.pojo.SceneRes;
import com.ihd.ihardware.pojo.SignsRes;
import com.ihd.ihardware.view.tzc.discovery.viewmodel.DiscoveryViewModel;

import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.base.BaseFragment;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class FXFragment extends BaseFragment<DiscoveryFxFragmentBinding, DiscoveryViewModel> {

    private Disposable msg;

    public FXFragment() {
    }

    public static FXFragment newInstance(String param1, String param2) {
        FXFragment fragment = new FXFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        setRx(AppConstans.SIGNS, new BaseConsumer<SignsRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(SignsRes mLiveData) {
                viewModel.setDTList(mLiveData.getData().getSignDetails());
                binding.dtRV.setHasFixedSize(true);
                binding.dtRV.setNestedScrollingEnabled(false);
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        setRx(AppConstans.ZJ, new BaseConsumer<SceneRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(SceneRes mLiveData) {
                viewModel.setZJList(mLiveData.getData());
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        setRx(AppConstans.MJ, new BaseConsumer<SceneRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(SceneRes mLiveData) {
                viewModel.setMJList(mLiveData.getData());
                binding.mjRV.setHasFixedSize(true);
                binding.mjRV.setNestedScrollingEnabled(false);
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        msg = RxBus.getInstance().tObservable(AppConstans.TAG, Bundle.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(Bundle b) throws Exception {
                        if (b.getInt("key") == 0) {
                            ObservableArrayList<SignsRes.DataBean.SignDetailsBean> sds = (ObservableArrayList<SignsRes.DataBean.SignDetailsBean>) viewModel.mDTAdapter.getItems();
                            for (int i = 0; i < sds.size(); i++) {
                                if (sds.get(i).getSignId().equals(b.getString("value"))) {
                                    if (b.getInt("type") == 0) {
                                        sds.get(i).setTagsNum(String.valueOf(Integer.parseInt(sds.get(i).getTagsNum()) - 1));
                                        sds.get(i).setTaged(false);
                                    } else {
                                        sds.get(i).setTagsNum(String.valueOf(Integer.parseInt(sds.get(i).getTagsNum()) + 1));
                                        sds.get(i).setTaged(true);
                                    }
                                    viewModel.mDTAdapter.notifyItemChanged(i);
                                }
                            }
                        } else
                            ToastUtils.showShort(getContext(), b.getString("value"));
                    }
                });

    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        binding.proRV.setAdapter(viewModel.mZJAdapter);
        viewModel.getZJList();

        binding.mjRV.setAdapter(viewModel.mMJAdapter);
        viewModel.getMJList();

        binding.dtRV.setAdapter(viewModel.mDTAdapter);
        viewModel.getDTList(1, 20);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.discovery_fx_fragment;
    }

    @Override
    protected Class getViewModelClass() {
        return DiscoveryViewModel.class;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (msg != null && !msg.isDisposed())
            msg.dispose();
    }
}
